// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Menu Control Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 2 : TYPE_MENUCONTROL

public alias saveRAM[257] : saved.spindash
public alias saveRAM[258] : saved.speedCap
public alias saveRAM[259] : saved.airSpeedCap
public alias saveRAM[260] : saved.spikeBehavior
public alias saveRAM[261] : saved.shieldType
public alias saveRAM[262] : saved.superStates
public alias saveRAM[263] : saved.Jumplock
public alias saveRAM[264] : saved.Supermusic
public alias saveRAM[265] : saved.Dropdash
public alias saveRAM[266] : saved.Instashield
public alias saveRAM[267] : saved.Peelout
public alias saveRAM[268] : saved.FlightCancel
public alias saveRAM[269] : saved.SonicMaxDash
public alias saveRAM[270] : saved.Speen
public alias saveRAM[271] : saved.Supercancel
public alias saveRAM[272] : saved.Drowning
public alias saveRAM[273] : saved.Timer
public alias saveRAM[274] : saved.region
public alias saveRAM[275] : saved.UFO
public alias saveRAM[276] : saved.rotate
public alias saveRAM[277] : saved.invincibilityType
public alias saveRAM[280] : saved.gogoplus
public alias saveRAM[281] : saved.lives
public alias saveRAM[282] : saved.bubble
public alias saveRAM[297] : saved.TailsMaxDash
public alias saveRAM[298] : saved.KnuxMaxDash
public alias saveRAM[299] : saved.AmyMaxDash


// Function declarations
reserve function MenuControl_Function0
reserve function MenuControl_Function1

// Static Values
public value MenuControl_value1 = 0;
public value MenuControl_value2 = 0;

// Tables
public table MenuControl_table1
	1, 9, 9, 1, 0, 6, 2, 3
end table

public table MenuControl_table2
	4, 1, 2, 6
end table

public table MenuControl_table0
	0, 0, 1, 0, 15, 1, 0, 1, 1, 1, 16, 1, 0, 2, 1, 2
	17, 1, 0, 4, 1, 3, 18, 2, 0, 5, 1, 4, 18, 2, 0, 6
	1, 5, 19, 2, 0, 8, 1, 6, 19, 3, 0, 9, 1, 7, 20, 3
	0, 10, 1, 8, 21, 3, 0, 12, 1, 9, 23, 4, 0, 13, 1, 10
	24, 4, 0, 14, 1, 11, 25, 4, 0, 16, 1, 12, 26, 5, 0, 17
	1, 13, 26, 5, 0, 18, 1, 14, 27, 5, 1, 0, 1, 15, 0, 6
	1, 1, 1, 16, 1, 6, 1, 2, 1, 17, 2, 10, 1, 4, 1, 18
	3, 7, 1, 6, 3, 0, 5, 8, 1, 9, 0, 0, 64, 0, 1, 10
	0, 0, 65, 0, 1, 11, 0, 0, 66, 0, 1, 12, 0, 0, 67, 0
	1, 13, 0, 0, 68, 0, 1, 14, 0, 0, 69, 0, 1, 15, 0, 0
	70, 0, 1, 18, 0, 0, 71, 0
end table


function MenuControl_Function0
	if object[+2].value0 < 8
		SetTableValue(object[+2].value0, 117, MenuControl_table0)
	end if
	GetTableValue(temp0, MenuControl_value1, MenuControl_table1)
	if temp0 == object[+2].value0
		MenuControl_value1++
		if MenuControl_value1 == 8
			MenuControl_value1 = 0
			stage.debugMode = 1
			PlaySfx(SfxName[Ring L], 0)
		end if
	else
		MenuControl_value1 = 0
	end if
	GetTableValue(temp0, MenuControl_value2, MenuControl_table2)
	if temp0 == object[+2].value0
		MenuControl_value2++
		if MenuControl_value2 == 4
			MenuControl_value2 = 0
			if saved.superStates == 1
				specialStage.emeralds = 127
			else
				specialStage.emeralds = 63
				specialStage.listPos = 6
			end if
			PlaySfx(SfxName[Emerald], 0)
			StopMusic()
		end if
	else
		MenuControl_value2 = 0
	end if
end function


function MenuControl_Function1
	if object.value4 == 0
		object.value4 = 30
		switch object[+2].value0
		case 0
			StopMusic()
			break
		case 1
			SetMusicTrack("GreenHill.ogg", 0, 0x9B0AA)
			PlayMusic(0)
			break
		case 2
			SetMusicTrack("Marble.ogg", 0, 0x18434)
			PlayMusic(0)
			break
		case 3
			SetMusicTrack("SpringYard.ogg", 0, 0x18968)
			PlayMusic(0)
			break
		case 4
			SetMusicTrack("Labyrinth.ogg", 0, 0x149DC)
			PlayMusic(0)
			break
		case 5
			SetMusicTrack("Starlight.ogg", 0, 0x1498C)
			PlayMusic(0)
			break
		case 6
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			PlayMusic(0)
			break
		case 7
			SetMusicTrack("Invincibility.ogg", 0, 0x9A68)
			PlayMusic(0)
			break
		case 8
			PlaySfx(SfxName[Life], 0)
			PauseMusic()
			ResetObjectEntity(25, TypeName[Music Event], 2, 0, 0)
			object[25].priority = PRIORITY_ACTIVE
			break
		case 9
			SetMusicTrack("SpecialStage.ogg", 0, 1)
			PlayMusic(0)
			break
		case 10
			SetMusicTrack("Titlescreen.ogg", 0, 0)
			PlayMusic(0)
			break
		case 11
			SetMusicTrack("Ending.ogg", 0, 0)
			PlayMusic(0)
			break
		case 12
			SetMusicTrack("Boss.ogg", 0, 1)
			PlayMusic(0)
			break
		case 13
			SetMusicTrack("Final.ogg", 0, 0x14AC8)
			PlayMusic(0)
			break
		case 14
			SetMusicTrack("ActComplete.ogg", 0, 0)
			PlayMusic(0)
			break
		case 15
			SetMusicTrack("GameOver.ogg", 0, 0)
			PlayMusic(0)
			break
		case 16
			SetMusicTrack("Continue.ogg", 0, 0)
			PlayMusic(0)
			break
		case 17
			SetMusicTrack("Credits.ogg", 0, 0)
			PlayMusic(0)
			break
		case 18
			SetMusicTrack("Drowning.ogg", 0, 0)
			PlayMusic(0)
			break
		end switch
	end if
end function


event ObjectMain
	object[+1].xpos = screen.xcenter
	object[+1].xpos += 118
	object[+2].xpos = object[+1].xpos
	switch object.state
	case 0
		object.value0 = 320
		SetClassicFade(0, 0, 0, object.value0)
		object[+1].ypos = 88
		object[+1].priority = PRIORITY_ACTIVE
		object[+2].ypos = 160
		object[+2].priority = PRIORITY_ACTIVE
		switch stage.playerListPos
		case 0
			object[+1].value0 = 1
			break
		case 1
			object[+1].value0 = 2
			break
		case 2
			object[+1].value0 = 3
			break
		case 3
			object[+1].value0 = 0
			break
		end switch
		object.state++
		break
	case 1
		if object.value0 > 0
			object.value0 -= 16
		else
			PlayMusic(0)
			object.state++
		end if
		SetClassicFade(0, 0, 0, object.value0)
		break
	case 2
		if inputDown.down == 1
			object.value1 = 0
			object.value2++
			if object.value2 == 1
				object.value3++
			end if
			object.value2 %= 12
		else
			if inputDown.up == 1
				object.value2 = 0
				object.value1++
				if object.value1 == 1
					object.value3--
				end if
				object.value1 %= 12
			else
				object.value1 = 0
				object.value2 = 0
			end if
		end if
		if object.value3 > 27
			object.value3 = 0
		end if
		if object.value3 < 0
			object.value3 = 27
		end if
		temp0 = object.value3
		temp0 *= 6
		temp0++
		GetTableValue(temp2, temp0, MenuControl_table0)
		temp0 += 3
		GetTableValue(temp1, temp0, MenuControl_table0)
		switch temp1
		default
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				object.value3 = temp1
			end if
			object[+1].value1 = 0
			object[+2].value1 = 0
			if inputPress.buttonA == 1
				inputPress.start = 1
			end if
			break
		case 64
			object[+1].value1 = 16
			object[+2].value1 = 0
			if inputPress.left == 1
				object[+1].value0--
			end if
			if inputPress.right == 1
				object[+1].value0++
			end if
			object[+1].value0 &= 3
			switch object[+1].value0
			case 0
				stage.playerListPos = 3
				break
			case 1
				stage.playerListPos = 0
				break
			case 2
				stage.playerListPos = 1
				break
			case 3
				stage.playerListPos = 2
				break
			end switch
			break
		case 65
			object[+1].value1 = 0
			object[+2].value1 = 0
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				saved.spindash ^= 1
				if saved.spindash == 1
					EditMenuEntry(MENU_2, "SPINDASH@@@@@*ON*", temp2, 1)
				else
					EditMenuEntry(MENU_2, "SPINDASH@@@@*OFF*", temp2, 1)
				end if
				WriteSaveRAM()
			end if
			break
		case 66
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				saved.speedCap ^= 1
				if saved.speedCap == 1
					EditMenuEntry(MENU_2, "GND SPD CAP@@*ON*", temp2, 1)
				else
					EditMenuEntry(MENU_2, "GND SPD CAP *OFF*", temp2, 1)
				end if
				WriteSaveRAM()
			end if
			break
		case 67
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				saved.airSpeedCap ^= 1
				if saved.airSpeedCap == 1
					EditMenuEntry(MENU_2, "AIR SPD CAP@@*ON*", temp2, 1)
				else
					EditMenuEntry(MENU_2, "AIR SPD CAP *OFF*", temp2, 1)
				end if
				WriteSaveRAM()
			end if
			break
		case 68
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				saved.spikeBehavior ^= 1
				if saved.spikeBehavior == 1
					EditMenuEntry(MENU_2, "S1 SPIKES@@@@*ON*", temp2, 1)
				else
					EditMenuEntry(MENU_2, "S1 SPIKES@@@*OFF*", temp2, 1)
				end if
				WriteSaveRAM()
			end if
			break
		case 69
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				if inputPress.left == 1
					saved.shieldType--
				end if
				if inputPress.right == 1
					saved.shieldType++
				end if
				saved.shieldType &= 1
				switch saved.shieldType
				case 0
					EditMenuEntry(MENU_2, "ITEMS@@@@@@@@*S1*", temp2, 1)
					break
				case 1
					EditMenuEntry(MENU_2, "ITEMS@@@@@@@@*S2*", temp2, 1)
					break
				end switch
			end if
			break
		case 70
			object[+1].value1 = 0
			object[+2].value1 = 0
			checkResult = inputPress.left
			checkResult |= inputPress.right
			if checkResult == 1
				saved.superStates ^= 1
				if saved.superStates == 1
					EditMenuEntry(MENU_2, "MAX EMERALDS *07*", temp2, 1)
				else
					EditMenuEntry(MENU_2, "MAX EMERALDS *06*", temp2, 1)
				end if
				WriteSaveRAM()
			end if
			break
		case 71
			if object.value4 > 0
				object.value4--
			end if
			object[+1].value1 = 0
			object[+2].value1 = 16
			if inputPress.buttonC == 1
				object[+2].value0 += 16
			end if
			if inputPress.left == 1
				object[+2].value0--
			end if
			if inputPress.right == 1
				object[+2].value0++
			end if
			object[+2].value0 &= 31
			if inputPress.buttonA == 1
				CallFunction(MenuControl_Function1)
				CallFunction(MenuControl_Function0)
			end if
			if inputPress.buttonB == 1
				CallFunction(MenuControl_Function1)
				CallFunction(MenuControl_Function0)
			end if
			break
		end switch
		temp0 = object.value3
		temp0 *= 6
		GetTableValue(temp1, temp0, MenuControl_table0)
		temp0++
		if temp1 == 0
			menu2.selection = -1
			GetTableValue(menu1.selection, temp0, MenuControl_table0)
		else
			menu1.selection = -1
			GetTableValue(menu2.selection, temp0, MenuControl_table0)
		end if
		temp0 += 4
		GetTableValue(BGAnimation_value0, temp0, MenuControl_table0)
		if inputPress.start == 1
			if object.value3 == 19
				PlaySfx(SfxName[Warp], 0)
				object.state = 4
				fadeColor = color7
				fadeColor <<= 16
				temp0 = color7
				temp0 <<= 8
				fadeColor += temp0
				fadeColor += color7
			else
				object.state = 3
			end if
		end if
		break
	case 3
		if object.value0 < 256
			music.volume -= 10
			object.value0 += 16
			SetClassicFade(0, 0, 0, object.value0)
		else
			StopMusic()
			temp0 = object.value3
			temp0 *= 6
			temp0 += 2
			GetTableValue(temp1, temp0, MenuControl_table0)
			temp0++
			GetTableValue(temp2, temp0, MenuControl_table0)
			stage.activeList = temp1
			stage.listPos = temp2
			LoadStage()
			SetClassicFade(0, 0, 0, 255)
		end if
		break
	case 4
		if object.value0 < 768
			music.volume -= 10
			object.value0 += 8
			SetClassicFade(color7, color7, color7, object.value0)
		else
			StopMusic()
			temp0 = object.value3
			temp0 *= 6
			temp0 += 2
			GetTableValue(temp1, temp0, MenuControl_table0)
			temp0++
			GetTableValue(temp2, temp0, MenuControl_table0)
			stage.activeList = temp1
			stage.listPos = temp2
			LoadPalette("SpecialStage.act",0,0,0,128)
			LoadStage()
			SetClassicFade(color7, color7, color7, 255)
		end if
		break
	end switch
end event


event ObjectDraw
	temp0 = screen.xcenter
	temp0 -= 10
	DrawMenu(MENU_1, temp0, 16)
	temp0 = screen.xcenter
	temp0 += 142
	DrawMenu(MENU_2, temp0, 16)
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Text.gif")
	foreach (TypeName[Menu Control], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
	options.stageSelectFlag = 1
	SetMusicTrack("Starlight.ogg", 0, 0x1498C)
	SetupMenu(MENU_1, 0, 3, 1)
	AddMenuEntry(MENU_1, "GREEN HILL 1", 1)
	AddMenuEntry(MENU_1, "2", 0)
	AddMenuEntry(MENU_1, "3", 0)
	AddMenuEntry(MENU_1, " ", 0)
	AddMenuEntry(MENU_1, "MARBLE 1", 1)
	AddMenuEntry(MENU_1, "2", 0)
	AddMenuEntry(MENU_1, "3", 0)
	AddMenuEntry(MENU_1, " ", 0)
	AddMenuEntry(MENU_1, "SPRING YARD 1", 1)
	AddMenuEntry(MENU_1, "2", 0)
	AddMenuEntry(MENU_1, "3", 0)
	AddMenuEntry(MENU_1, " ", 0)
	AddMenuEntry(MENU_1, "LABYRINTH 1", 1)
	AddMenuEntry(MENU_1, "2", 0)
	AddMenuEntry(MENU_1, "3", 0)
	AddMenuEntry(MENU_1, " ", 0)
	AddMenuEntry(MENU_1, "STAR LIGHT 1", 1)
	AddMenuEntry(MENU_1, "2", 0)
	AddMenuEntry(MENU_1, "3", 0)
	AddMenuEntry(MENU_1, " ", 0)
	SetupMenu(MENU_2, 0, 3, 1)
	AddMenuEntry(MENU_2, "SCRAP BRAIN 1", 1)
	AddMenuEntry(MENU_2, "2", 0)
	AddMenuEntry(MENU_2, "3", 0)
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, "FINAL ", 1)
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, "SPECIAL STAGE ", 1)
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, "PLAYER@@@@@@@*@@*", 1)
	if saved.spindash == 1
		AddMenuEntry(MENU_2, "SPINDASH@@@@@*ON*", 1)
	else
		AddMenuEntry(MENU_2, "SPINDASH@@@@*OFF*", 1)
	end if
	if saved.speedCap == 1
		AddMenuEntry(MENU_2, "GND SPD CAP@@*ON*", 1)
	else
		AddMenuEntry(MENU_2, "GND SPD CAP@*OFF*", 1)
	end if
	if saved.airSpeedCap == 1
		AddMenuEntry(MENU_2, "AIR SPD CAP@@*ON*", 1)
	else
		AddMenuEntry(MENU_2, "AIR SPD CAP@*OFF*", 1)
	end if
	if saved.spikeBehavior == 1
		AddMenuEntry(MENU_2, "S1 SPIKES@@@@*ON*", 1)
	else
		AddMenuEntry(MENU_2, "S1 SPIKES@@@*OFF*", 1)
	end if
	switch saved.shieldType
	case 0
		AddMenuEntry(MENU_2, "ITEMS@@@@@@@@*S1*", 1)
		break
	case 1
		AddMenuEntry(MENU_2, "ITEMS@@@@@@@@*S2*", 1)
		break
	case 2
		AddMenuEntry(MENU_2, "ITEMS@@@@@*S1+S3*", 1)
		break
	case 3
		AddMenuEntry(MENU_2, "ITEMS@@@@@*S2+S3*", 1)
		break
	end switch
	if saved.superStates == 0
		AddMenuEntry(MENU_2, "MAX EMERALDS *06*", 1)
	else
		AddMenuEntry(MENU_2, "MAX EMERALDS *07*", 1)
	end if
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, " ", 0)
	AddMenuEntry(MENU_2, "SOUND TEST *@@*", 1)
	menu1.selection = 0
	menu2.selection = -1
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
